package com.neusoft.neuiotms.dcam.fixed.converter;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.ContractDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Contract;
import com.neusoft.neuiotms.dcam.fixed.model.ContractItem;
import com.neusoft.neuiotms.dcam.fixed.repository.ContractRepository;

@Component
public class ContractConverter extends AbstractConverter<Contract, ContractDTO> {

    @Autowired
    private ContractRepository contractRepository;
    @Autowired
    private ContractItemConverter contractItemConverter;
    //    @Autowired
    //    private ManufacturerRepository manufacturerRepository;

    @Override
    public Contract toModel(final ContractDTO dto) {
        Contract model;

        if (dto.isNew()) {
            model = new Contract();
        } else {
            model = this.contractRepository.getOne(dto.getId());
        }

        model.setCode(dto.getCode());
        model.setName(dto.getName());
        model.setPostCode(dto.getPostCode());
        model.setSigningDate(dto.getSigningDate());
        model.setQualityAssurance(dto.getQualityAssurance());
        model.setDeliveryDeadline(dto.getDeliveryDeadline());
        model.setTransport(dto.getTransport());
        model.setPacking(dto.getPacking());
        model.setArrivalAddress(dto.getArrivalAddress());
        model.setPaymentTerms(dto.getPaymentTerms());
        model.setRemark(dto.getRemark());
        model.setPaymentStatus(dto.getPaymentStatus());
        model.setBelongs(dto.getBelongs());
        model.setBill(dto.getBill());
        model.setWhetherArrival(dto.getWhetherArrival());
        model.setType(dto.getType());
        model.setAttachment(dto.getAttachment());
        model.setManufacturerName(dto.getManufacturerName());
        model.setAmount(dto.getAmount());

        // 供货商设置
        //        if (null != dto.getManufacturerId()) {
        //            final Manufacturer manufacturer = this.manufacturerRepository.getOne(dto.getManufacturerId());
        //            model.setManufacturer(manufacturer);
        //        }
        return model;
    }

    @Override
    public ContractDTO toDTO(final Contract model, final boolean forListView) {

        final ContractDTO dto = new ContractDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setName(model.getName());
        dto.setPostCode(model.getPostCode());
        dto.setSigningDate(model.getSigningDate());
        dto.setQualityAssurance(model.getQualityAssurance());
        dto.setDeliveryDeadline(model.getDeliveryDeadline());
        dto.setTransport(model.getTransport());
        dto.setPacking(model.getPacking());
        dto.setArrivalAddress(model.getArrivalAddress());
        dto.setPaymentTerms(model.getPaymentTerms());
        dto.setRemark(model.getRemark());
        dto.setPaymentStatus(model.getPaymentStatus());
        dto.setBelongs(model.getBelongs());
        dto.setBill(model.getBill());
        dto.setWhetherArrival(model.getWhetherArrival());
        dto.setType(model.getType());
        dto.setAttachment(model.getAttachment());
        dto.setAmount(model.getAmount());

        //        if (model.getManufacturer() != null) {
        //            dto.setManufacturerName(model.getManufacturer().getName());
        //            dto.setManufacturerAddress(model.getManufacturer().getAddress());
        //            dto.setManufacturerContact(model.getManufacturer().getPerson());
        //            dto.setManufacturerTelephone(model.getManufacturer().getPhone());
        //            dto.setManufacturerBank(model.getManufacturer().getBank());
        //            dto.setManufacturerAccount(model.getManufacturer().getAccountNumber());
        //            dto.setManufacturerTaxId(model.getManufacturer().getTaxId());
        //        }
        dto.setManufacturerName(model.getManufacturerName());

        if (!forListView) {
            final List<ContractItem> itemModels = model.getItems();
            itemModels.stream().map(i -> this.contractItemConverter.toDTO(i)).forEach(dto.getItems()::add);
        }

        return dto;
    }

}
